<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class CategoryController extends CoreController {

	public function index(){
         if (I('get.p')) {
            $page=I('get.p');
        }else{
            $page = 1;
        }
        $cateModel = D('Category');
        $this->listStr=$cateModel->getCateTableHtml();
        // $count = $cateModel->where('status=1')->page($page,2)->select();
        // $count = $cateModel->where('status=1')->count();
        // $page =  new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $show = $page->show();// 分页显示输出
        // $this->assign('page',$show);
        $this->assign('addText','添加分类');
        $this->assign('addNew',U('add'));
        $this->assign('edit',U('add'));
        $this->assign('del',U('delUser'));
        $this->display();
    }

    public function add(){
            $id=I('get.id',0);
            $cateModel = D('Category');
            if($id>0){
                //查询产品
                $category = $cateModel->find($id);
                $this->assign('category',$category);
                $this->assign('id',$id);
            }
            $this->optionStr=$cateModel->getCateOptionHtml();
	        $this->assign('action',U('save'));
	        $this->assign('cancel',U('index'));
            $this->assign('upload',U('upload'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
            $this->setStaticFile('plugin/webupload/webuploader.css','head','css');
            $this->setStaticFile('plugin/webupload/webuploader.js','head','js');
            $this->setStaticFile('plugin/ueditor/ueditor.config.js','head','js');
            $this->setStaticFile('plugin/ueditor/ueditor.all.js','head','js');
	        $this->display();
        
    }

    public function save(){
    	if(IS_POST){
    		$id = I('post.id',0,'intval');
    		$data = array(
    			'name' => I('post.name'),
                'keyword' => I('post.keyword'),
                'description' => I('post.description'),
                'pid' => I('post.pid',0),
                'status'=>I('post.status',0,'intval'),
    			);
            $cateModel = D('Category');

            if(I('post.pid',0)>0){
                $pid = I('post.pid',0);
                $pCate = $cateModel->find($pid);
                $data['level']=$pCate['level']+1;
            }
            
            if($cateModel->create($data)){
                if($id>0){
                    $cateModel->where('id='.$id)->save();
                    $pid=$id;
                }else{
                    $pid=$cateModel->add();
                }
                
                if($pid>0){
                    $backdata['status']='OK';
                    $backdata['msg']='保持成功';
                    $backdata['url']=U('index');
                    $this->ajaxReturn($backdata);
                }else{
                    $backdata['status']='error';
                    $backdata['msg']=$productModel->getError();
                    $this->ajaxReturn($backdata);
                }
            }else{
                $backdata['status']='error';
                $backdata['msg']=$productModel->getError();
                $this->ajaxReturn($backdata);
            }
    	}else{

        }
        
    }
}